//宫格
import uniGrid from "/uni_modules/uni-grid/components/uni-grid/uni-grid.vue";
// 公共头部
import myHeader from "/pages/headerfoot/pages/header.vue";
//语言
import langMatch from '/utils/langmatch.js';
//cache缓存
import Cache from '@/utils/cache.js';
//打帮赛方法
import hitmatchcommon from '@/common/hitmatchcommon.js';
// 基础方法
import Base from "/common/base.js";

export default {
	props: {
		code: String,
		project_make_id: String,
		project_id: String,
		gametype: String,
		initiator: String
	},
	components: {
		myHeader,
		uniGrid,
	},
	data() {
		return {
			value: '',
			digit: 1, //位数
			string: '', //字符串
			nowclick: 0, //位数
			nowupnumber: 0, //当前上数字
			nowdownnumber: 0, //当前下数字
			langMatch: langMatch.lang, //语言1
			times: 0, //分钟
			seconds: '00', //秒
			answertime: 0, //答题用时
			timer: {}, //时间
			ifshow: true, //是否显示
			totalscore: 0, //总分数
			score: 0, //获取分数
			ifpass: false, //是否弹出闯关界面:false=不弹出，true=弹出
			btna: ['返回项目选择', '查看排行榜', '再次打榜','提交分数'],
			my_answer:[],//我的答案
			coding:0,//编码
			project_user_achievement_end:0, //提交分数 0=未提交 1=提交
		}
	},
	mounted() {
		var that = this;
		console.log(that.gametype);//对战类型
		console.log(that.initiator);//发起人
		
		//console.log(that.code);//编码
		if(that.code){
			that.coding=that.code;
		}else{
			 that.coding=new Date().getTime();//设置编码
		}
		//console.log(that.project_make_id);//赛事
		if(that.project_make_id){
			Cache.set('project_make_id',that.project_make_id)
		}
		//console.log(that.project_id);//项目
		if(that.project_id){
			Cache.set('project_id',that.project_id)
		}
		that.getMakeProjecParameter();//得到项目配置
	},
	methods: {
		//开始游戏
		startGame() {
			var that = this;
			that.ifshow = false; //隐藏弹出遮罩
			that.judgmentdigit(that.nowupnumber);
			that.countdown(); //计算器
		},
		//得到项目配置
		async getMakeProjecParameter() {
			var that = this;
			var data = {};
			data['project_id'] = Cache.get('project_id');
			data['make_user_id'] = Cache.get('user_id');
			data['make_id'] = Cache.get('project_make_id');
			hitmatchcommon.getMakeProjecParameterLibMethod(data).then(res => {
				if (res) {
					 var data=res.make_project_parameter_list[1];
					    that.digit=data.level;
						if(data.seconds<=9){
							that.seconds="0"+data.seconds;
						}
						that.times=data.times;
						that.score=data.score;
				}
			});
		},
		// //计算器
		// timeDown() {
		// 	var that = this;
		// 	that.timer = setInterval(() => {
		// 		that.answertime++; //答题用时
		// 		if (that.seconds >= 59) {
		// 			that.seconds = "00"; //默认
		// 			that.times++; //加分钟
		// 		} else {
		// 			that.seconds=parseInt(that.seconds)+1; //加秒
		// 			if(that.seconds<=9){
		// 				that.seconds='0'+that.seconds;//补0
		// 			}
		// 		}
		// 	}, 1000);
		// },
		//倒计时
		countdown() {
			var that = this;
			var times = parseInt(that.times); //分钟
			var seconds = parseInt(that.seconds); //秒
			that.timer = setInterval(() => {
				that.answertime++; //答题用时
				if (seconds <= 0) {
					if (times > 0) {
						that.seconds = seconds = 59;
						that.times = --times; //减分钟
					} else {
						//时间结束
						that.submit(); //提交
						clearInterval(that.timer)
					}
				} else {
					--seconds; //减秒数
					if (seconds <= 9) {
						that.seconds = '0' + seconds;
					} else {
						that.seconds = seconds;
					}
				}
			}, 1000);
		},
		//计算分数
		calculateTheScore() {
			var that = this;
			that.totalscore = that.totalscore + that.score; //分数添加
		},
		//点击判断
		clickList(e) {
			var that = this;
			//返回项目选择
			if (e == '0') {
				uni.reLaunch({
					url:hitmatchcommon.automaticurl('matcheslist')+"?id="+ Cache.get('project_make_id'),
				})
			}
			//排行榜
			if (e == '1') {
				 uni.reLaunch({
				 	url:hitmatchcommon.automaticurl('ranking')+"?id="+ Cache.get('project_make_id'),
				 })
			}
			//再次打榜
			if (e == 2) {
				that.ifshow = true; //隐藏弹出遮罩
				that.ifpass=false;
				that.value = '';
				that.digit = 1; //位数
				that.string = ''; //字符串
				that.nowclick = 0; //位数
				that.nowupnumber = 0; //当前上数字
				that.nowdownnumber = 0; //当前下数字
				that.times= 9; //分钟
				that.seconds= '00'; //秒
			}
			//提交分数
			if (e == '3') {
				//提交分数 0=未提交 1=提交
				that.project_user_achievement_end=1;
				that.submit();
				Base.publicshowtoastsuccess('提交成功',hitmatchcommon.automaticurl('matcheslist') + "?id=" + Cache.get('project_make_id'));
			}
		},
		//提交
		submit() {
			var that = this;
			var data = {};
			data['project_id'] = Cache.get('project_id');
			data['make_user_id'] = Cache.get('user_id');
			data['make_id'] = Cache.get('project_make_id');
			data['questions_answer'] = that.my_answer;//问题
			data['my_answer'] = that.my_answer;//我的答案
			data['answertime'] =that.answertime;//答题用时
			data['allscore'] = that.totalscore;//我的分数
			data['level'] =1;//关卡
			data['clearance'] = 1;//是否过关
			data['topic'] =1;//题数
			data['make_project_user_code'] = that.coding; //编码
			data['project_user_achievement_end'] = that.project_user_achievement_end; //提交分数 0=未提交 1=提交
			
			hitmatchcommon.setProjectUserAchievementLibMethod(data);//提交分数
			
			that.ifpass = true; //退出闯关
		},
		//点击
		number(e) {
			var that = this;
			that.nowclick++;
			that.string = String(that.string) + String(e); //重新赋值	
			var addop = that.string.length; //当前点击后text长度	 
			that.value = that.string.substring(addop - that.nowclick); //需要遍历的数据
			that.my_answer.push(that.value);

			//重新赋值
			if (that.nowclick == that.digit) {
				that.nowclick = 0;
				that.nowdownnumber = that.value;
				that.judgmentorder(); //判断顺序
			}
			that.judgmentdigit(that.nowupnumber);
		},
		//提交
		confirmsubmit() {
			var that = this;
			uni.showModal({
				title: '提示',
				content: '确定提交吗？',
				success: function(res) {
					if (res.confirm) {
						that.submit(); //提交
					} else if (res.cancel) {
						console.log('用户点击取消');
					}
				}
			});
		},
		//位数
		judgmentdigit(e) {
			var that = this;
			if (e >= 9 && e < 99 && that.digit == 1) {
				that.digit++;
			} else if (e >= 99 && e < 999 && that.digit == 2) {
				that.digit++;
			} else if (e >= 999 && e < 9999 && that.digit == 3) {
				that.digit++;
			} else if (e >= 9999 && e < 99999 && that.digit == 3) {
				that.digit++;
			}
		},
		//判断顺序
		judgmentorder() {
			var that = this;
			var ifequality = parseInt(that.nowupnumber) + parseInt(1);
			if (ifequality == that.nowdownnumber) {
				that.nowupnumber = that.nowdownnumber;
				that.calculateTheScore();
			} else {
				that.submit(); //提交
				//时间结束
				clearInterval(that.timer)
			}
		}
	}
}